package io.github.wslxm.springbootplus2.manage.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.github.wslxm.springbootplus2.client.sys.model.vo.USysBannerVO;
import io.github.wslxm.springbootplus2.constant.CacheKey;
import io.github.wslxm.springbootplus2.core.base.model.BasePage;
import io.github.wslxm.springbootplus2.core.base.service.impl.BaseServiceImpl;
import io.github.wslxm.springbootplus2.core.enums.BaseDic;
import io.github.wslxm.springbootplus2.core.utils.XjBeanUtil;
import io.github.wslxm.springbootplus2.manage.sys.mapper.SysBannerMapper;
import io.github.wslxm.springbootplus2.manage.sys.model.dto.SysBannerDTO;
import io.github.wslxm.springbootplus2.manage.sys.model.entity.SysBanner;
import io.github.wslxm.springbootplus2.manage.sys.model.query.SysBannerQuery;
import io.github.wslxm.springbootplus2.manage.sys.model.vo.SysBannerVO;
import io.github.wslxm.springbootplus2.manage.sys.service.SysBannerService;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * banner表
 * <p>
 *  ::本代码由[兮家小二]提供的代码生成器生成,如有问题,请手动修改 ::作者CSDN:https://blog.csdn.net/qq_41463655 
 * </p>
 * @author wangsong
 * @email 1720696548@qq.com
 * @date 2020-08-23 23:14:01
 */
@Service
public class SysBannerServiceImpl extends BaseServiceImpl<SysBannerMapper, SysBanner> implements SysBannerService {

    @Override
    public BasePage<SysBannerVO> findPage(SysBannerQuery query) {
        LambdaQueryWrapper<SysBanner> queryWrapper = new LambdaQueryWrapper<SysBanner>()
                .orderByAsc(SysBanner::getPosition)
                .orderByAsc(SysBanner::getSort)
                .orderByDesc(SysBanner::getCreateTime)
                .eq(query.getPosition() != null, SysBanner::getPosition, query.getPosition())
                .eq(query.getDisable() != null, SysBanner::getDisable, query.getDisable())
                .eq(StringUtils.isNotBlank(query.getName()), SysBanner::getName, query.getName());
        return XjBeanUtil.pageVo(this.page(new Page<>(query.getCurrent(), query.getSize()), queryWrapper), SysBannerVO.class);
    }


    @Override
    @Cacheable(cacheResolver = CacheKey.XJ_RESOLVER,value = CacheKey.BANNER_BY_POSITION, key = "#position", unless = "#result == null")
    public List<USysBannerVO> findByPosition(Integer position) {
        SysBannerQuery query = new SysBannerQuery();
        query.setPosition(position);
        query.setDisable(BaseDic.Whether.V0.getValue());
        List<SysBannerVO> records = this.findPage(query).getRecords();
        return XjBeanUtil.listVo(records,USysBannerVO.class);
    }

    @Override
    @CacheEvict(cacheResolver = CacheKey.XJ_RESOLVER,value = CacheKey.BANNER_BY_POSITION, key = "#dto.position", allEntries = true)
    public String insert(SysBannerDTO dto) {
        SysBanner entity = dto.convert(SysBanner.class);
        boolean b = this.save(entity);
        return entity.getId();
    }

    @Override
    @CacheEvict(cacheResolver = CacheKey.XJ_RESOLVER,value = CacheKey.BANNER_BY_POSITION, key = "#dto.position", allEntries = true)
    public boolean upd(String id, SysBannerDTO dto) {
        SysBanner entity = dto.convert(SysBanner.class);
        entity.setId(id);
        return this.updateById(entity);
    }

    @Override
    @CacheEvict(cacheResolver = CacheKey.XJ_RESOLVER,value = CacheKey.BANNER_BY_POSITION, allEntries = true)
    public boolean del(String id) {
        return this.removeById(id);
    }
}
